<template>
	<div class="buynow_div">
		<div class="fl btn_coll_area" style="width: 40%;">
			<div class="kefu_div fl">
				<p class="icon-app_coll"></p>
				<p>客服</p>
			</div>
			<div class="coll_div fl">
				<p class="icon-app_coll"></p>
				<p>收藏</p>
			</div>
		</div>
		<div class="fl btn_buy_area" style="width: 60%;">
			<p class="fl" @click="btn_addCart" :bind_data_addCart="addCartRes">加入购物车</p>
			<p class="fl" @click="btn_buynow">立即购买</p>
		</div>
		<alert-com :alertMsg="alertWord" v-show="alertWord != ''"></alert-com>
	</div>
</template>
<script>
	import alert from '../public/alert.vue'
	export default{
		name:'buynow',
		data(){
			return {
				alertWord: '',
				btnType:'', // 得到当前点击的是哪个按钮。因为，如果点击了立即购买是需要去跳转到订单列表的。拿到状态好做判断是否需要跳转
				obj_gowuche:'', //
				//active:'SP',
			}
		},
		components:{
			"alert-com": alert,
		},
		methods:{
			//添加购物车
			btn_addCart:function(){
				//要添加购物车先判断是否已登录
				var l = window.localStorage['_login'] || window.sessionStorage['_login'];
				if(!l){
					this.$router.push({ path: '/login' });

				}else{
					if(this.$store.state.goods.data_goodNum == 0){
						this.alertWord = 'good num  0';
						var self=this;  
						setTimeout(function(){
	 						self.alertWord = '';
	 					},3000);
						return false;
					}else{
						this.btnType = "btnType_add";
						this.$store.dispatch('get_cart');
						
					}
				}
			},
			//立即购买
			btn_buynow:function(){
				var l = window.localStorage['_login'] || window.sessionStorage['_login'];
				if(!l){
					this.$router.push({ path: '/login' });

				}else{
					if(this.$store.state.goods.data_goodNum == 0){
						this.alertWord = 'good num  0';
						var self=this;  
						setTimeout(function(){
	 						self.alertWord = '';
	 					},3000);
						return false;
					}else{
						this.btnType = "btnType_nowBuy";
						this.$store.dispatch('get_cart');
						
					}
				}
			}
		},
		computed:{
			addCartRes(){
				console.log(this.$store.state.goods.data_cart)
				if (JSON.stringify(this.$store.state.goods.data_cart) == "{}") {
					return false;
				}else{
					var _res = this.$store.state.goods.data_cart;
					if(_res.success == false){
						this.alertWord = _res.message;
						var self=this;  
	 					setTimeout(function(){
	 						self.alertWord = '';
	 					},3000);
					}else{
						if(this.btnType == 'btnType_nowBuy'){ // _res

							//this.obj_gowuche = _res.data.cartId;
							console.log(this.obj_gowuche);
							this.$router.push({
								path: "/orderprev",
								name:  "orderprev",
								params:{
									name:"orderprev",
									goodId: _res.data.cartId//   参数名称自定义，obj: 需要传递的参数
								}
							})
						}else{
							this.alertWord ='add  success';
							var self=this;  
							setTimeout(function(){
								self.alertWord = '';
		 					},3000);
						}
					}
				}
				return this.$store.state.goods.data_cart;
			}
		},
		mounted:function(){
			//this.getGoodInfo()
			
		},
		watch:{
			//"$route": "getGoodInfo" 
		}


	}
</script>
<style rel="stylesheet" scoped>
	.buynow_div{
		position: fixed;
		bottom: 0;
		width: 100%;
		max-width: 20.0rem;
		z-index: 1;
		height: 1.2rem;
		background-color: #FFF;
	}
	.goodinfo_header{
		height: 1.3333rem;
		line-height: 1.3333rem;
	}
	.btn_coll_area{
		height: 1.2rem;
	}
	.btn_coll_area div{
		width: 50%;
		height: 1.2rem;
		text-align: center;
		font-size: 0.32rem;
		color: #A9A9A9;
	}
	.btn_coll_area div p:first-child{
		height: 0.6rem;
		line-height: 0.6rem;
		font-size: 0.4267rem;
	}
	.btn_coll_area div p:last-child{
		height: 0.6rem;
		line-height: 0.6rem;
	}
	.btn_buy_area {
		overflow: hidden;
	}
	.btn_buy_area p{
		width: 50%;
		height: 1.2rem;
		line-height: 1.2rem;
		text-align: center;
		font-size: 0.3733rem;
		color: #FFFFFF;
	}
	.btn_buy_area p:first-child{
		background-color: #575757;
	}
	.btn_buy_area p:last-child{
		background-color: #FF7D37;
	}
</style>